package fs.component.popup {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author F.S.
	 */
	public class AbstractDialog extends Sprite {
		private var _totalLayer:Sprite;
		
		private var _dialogLayer:Sprite;
		private var _contentLayer:Sprite;
		private var _tfTitle:TextField;
		
		public function AbstractDialog() {
			_totalLayer = new Sprite();
			addChild(_totalLayer);
			
			_dialogLayer = new Sprite();
			_dialogLayer.graphics.beginFill(0xC0C0C0);
			_dialogLayer.graphics.drawRect(0, 0, 100, 100);
			_dialogLayer.graphics.endFill();
			_totalLayer.addChild(_dialogLayer);
			
			_contentLayer = new Sprite();
			_contentLayer.x = 5;
			_contentLayer.y = 35;
			_totalLayer.addChild(_contentLayer);
			
			_tfTitle = new TextField();
			_tfTitle.maxChars = 128;
			_tfTitle.height = 20;
			_tfTitle.background = true;
			_tfTitle.backgroundColor = 0x808080;

			_totalLayer.addChild(_tfTitle);
			
			resize();
		}
		
		protected function resize():void {
			addEventListener(Event.ENTER_FRAME, onDelayed);			
		}
		
		private function onDelayed(e:Event):void {
			removeEventListener(Event.ENTER_FRAME, onDelayed);	
			rearrangeUi();
			
			var w:int = _contentLayer.width + 10;
			_tfTitle.width = _dialogLayer.width = w<50?50:w;
			_dialogLayer.height = _contentLayer.height + 50;
			
			_totalLayer.x = -_totalLayer.width / 2;
			_totalLayer.y = -_totalLayer.height / 2;
		}
		
		//{ region public function		
		public function addContent(child:DisplayObject):DisplayObject {
			return _contentLayer.addChild(child);
		}
		public function removeContent(child:DisplayObject):DisplayObject {
			return _contentLayer.removeChild(child);
		}
		//} endregion
		
		//{ region virtual function 
		protected function rearrangeUi():void {}
		//} endregion
		
		//{ region getters and setters
		public function get title():String { return _tfTitle.text };
		public function set title(value:String):void {
			_tfTitle.text = value;
			//_tf_content.width = stage.stageWidth / 5;
			//_tf_title.x = stage.stageWidth / 5;
			
			resize();
		}		
		//} endregion
	}

}